package com.zhu.androidsample.business.phonelocation.presenter;

import com.zhu.androidsample.business.phonelocation.PhoneLocationActivity;
import com.zhu.androidsample.business.phonelocation.bean.PhoneLocationBean;
import com.zhu.androidsample.common.BaseActivity;
import com.zhu.androidsample.common.BasePresenter;
import com.zhu.androidsample.common.UrlMap;
import com.zhu.androidsample.util.JsonBeanUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by ZCP on 2015/11/19.
 * mail:43426073@qq.com
 */
public class PhoneLocationPresenter extends BasePresenter {
    BasePresenter.Listener phoneLocationListener = new BasePresenter.Listener() {
        @Override
        public void onResponse(String o) {
            super.onResponse(o);
            try {
                JSONObject object;
                object = new JSONObject(o).optJSONObject("showapi_res_body");
                PhoneLocationBean bean = (PhoneLocationBean) JsonBeanUtil.json2Bean(object, PhoneLocationBean.class);
                updateActivity(PhoneLocationActivity.UPDATE_LOCATION, bean);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    };

    public PhoneLocationPresenter(BaseActivity activity) {
        super(activity);
    }

    /**
     * Presenter的业务逻辑
     * 功能：请求数据，查询号码归属地
     *
     * @param number 电话号码
     */
    public void searchPhoneLocation(String number) {
        Map<String, String> params = new HashMap<>();//新建一个参数容器
        params.put("num", number);//添加一个num参数，即电话号码
        postRequest(UrlMap.PHONE_LOCATION, params, phoneLocationListener);//开始请求数据
    }

}
